package java3_15.shangchang;

import java3_15.gou.Goodpeople;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Market_test {

    Member[] members = new Member[30];

    List<Member> memberList = new ArrayList<>();

    Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
//        Goodpeople.test("asd","123");

        Market_test test = new Market_test();
        Supermarket market = new Supermarket();
        market.name = "大润发购物广场";
        market.address = "马鞍山路";
        System.out.println("欢迎来到位于"+market.address+"的"+market.name);
        test.test();
        /*Member member = new Member();
        System.out.println("请输入会员卡号：");
        member.card = test.scanner.nextInt();
        System.out.println("请输入会员姓名：");
        member.name = test.scanner.next();
        System.out.println("请输入会员电话：");
        member.phoneNumber = test.scanner.nextInt();
        System.out.println("请输入会员积分：");
        member.score = test.scanner.nextInt();
        test.memberList.add(member);
        System.out.println(test.memberList);*/
    }

    public void test() {
        while (true){
            Member member = new Member();
            System.out.println("欢迎进入大润发商场！"+"\n"+"1.新会员开卡  2.查看会员信息   3.会员涨分  4.添加新会员  5.礼品兑换  6.退出");
            int x = scanner.nextInt();
            switch (x){
                case 1:{
                    System.out.println("请输入会员卡号：");
                    member.card = scanner.nextInt();
                    System.out.println("请输入会员姓名：");
                    member.name = scanner.next();
                    System.out.println("请输入会员电话：");
                    member.phoneNumber = scanner.nextInt();
                    System.out.println("请输入会员积分：");
                    member.score = scanner.nextInt();
                    boolean k = add(member);
                    System.out.println(k);
                    break;
                }
                case 2:{
                    search();
                    break;
                }
                case 3:{
                    System.out.println("请输入会员卡号：");
                    int card = scanner.nextInt();
                    System.out.println("请输入要增加的分数：");
                    int fen = scanner.nextInt();
                    boolean inc = inc(card,fen);
                    if(inc){
                        System.out.println("添加成功！");
                    }else {
                        System.out.println("卡号不存在！");
                    }
                    break;
                }
                case 5:{
                    System.out.println("请输入会员卡号：");
                    int card = scanner.nextInt();
                    System.out.println("请输入礼品类型：");
                    int lipingleixing = scanner.nextInt();
                    System.out.println(duihuan(card, lipingleixing));
                }
                case 6:{
                    return;
                }
            }
        }
    }

    public  boolean add(Member c) {
        if (checkCardNoRepeat(c.card)) {
            return false;
        }
        for (int i = 0; i < members.length; i++) {
            if (members[i] == null) {
                members[i] = c;
                return true;
            }
        }
        return false;
    }

    public boolean checkCardNoRepeat(int card) {

        for (int i = 0; i < members.length; i++) {
            Member b = members[i];
            if (b==null){
                continue;
            }
            if (b.card == card) {
                System.out.println("卡号重复，添加失败!");
                return true;
            }
        }
        return false;
    }

    public String duihuan(int card, int lipingleixing) {
        for (int i = 0; i < members.length; i++) {
            Member c = members[i];
            if (c.card == card) {
                switch (lipingleixing) {
                    case 1: {
                        if (c.score >= 100) {
                            c.score = c.score - 100;
                            return "兑换餐巾纸成功";
                        } else {
                            return "积分不够，兑换失败";
                        }
                    }
                    case 2: {
                        if (c.score >= 400) {
                            c.score = c.score - 400;
                            return "兑换剃须刀成功";
                        } else {
                            return "积分不够，兑换失败";
                        }
                    }
                    case 3: {
                        if (c.score >= 1000) {
                            c.score = c.score - 1000;
                            return "兑换炒锅成功";
                        } else {
                            return "积分不够，兑换失败";
                        }
                    }
                    default:{
                        System.out.println("输入有误，兑换失败！");
                    }
                }
            }

        }
        return "卡号不存在！兑换失败。";
    }

    public boolean inc(int card, int fen) {
        for (int i = 0; i < members.length; i++) {
            Member a = members[i];
            if (a == null) {
                continue;
            }
            if (a.card == card) {
                a.score = a.score + fen;
                return true;
            }
        }
        return false;
    }


    public void search() {
        for (int i = 0; i < members.length; i++) {
            if (members[i] == null) {
                return;
            }
            System.out.print(members[i].name + "," + members[i].card + "," + members[i].phoneNumber + "," + members[i].score);
            System.out.println();
        }
    }


}
